﻿using FluentAssertions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using NewRetail.Application.Components.Platform;
using NewRetail.Application.Components.Platform.Entity;
using NewRetail.Application.Components.Warehouse;
using NewRetail.Application.Core;
using System;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;

namespace NewRetail.Api.Controllers
{
    [NewRetailRoute("payment-receipt")]
    public class PaymentReceiptController : BaseController<PaymentReceipt, PaymentReceiptPagingDto>
    {
        private readonly IWarehouseGoodsService warehouseGoodsService;
        private readonly IPaymentReceiptService paymentReceiptService;

        public PaymentReceiptController(
            IWarehouseGoodsService warehouseGoodsService,
            IPaymentReceiptService paymentReceiptService)
        {
            this.warehouseGoodsService = warehouseGoodsService;
            this.paymentReceiptService = paymentReceiptService;
        }

        protected override async Task<IQueryable<PaymentReceipt>> FilterQueryPaging(DbContext db, IQueryable<PaymentReceipt> query, PaymentReceiptPagingDto dto)
        {
            if (!string.IsNullOrWhiteSpace(dto.receiptNo))
            {
                query = query.Where(x => x.receiptNo == dto.receiptNo || x.receiptNo.Contains(dto.receiptNo));
            }
            if (dto.Date != null)
            {
                var start = dto.Date.Value.Date.AddHours(-8);
                var end = start.AddDays(1);

                query = query.Where(x => x.addTime >= start && x.addTime < end);
            }
            await Task.CompletedTask;
            return query;
        }

        public override async Task<ResponseEntity<PagedResultDto<PaymentReceipt>>> QueryPaging([FromBody] PaymentReceiptPagingDto dto)
        {
            var res = await base.QueryPaging(dto);

            if (res?.Data?.Items?.Any() ?? false)
            {
                res.Data.Items = await this.paymentReceiptService.AttachSuppiler(res.Data.Items.ToArray());
            }

            return res;
        }

        [HttpPost("save-multiple")]
        public async Task<ResponseEntity<PaymentReceipt[]>> SaveMultiple([FromBody] ReceiptNoItemsDto<PaymentReceipt> data)
        {
            var res = await this.Repo.SaveReceiptItems(this.HttpContext.RequestServices, data, "PR");
            return new ResponseEntity<PaymentReceipt[]>().SetData(res.Items);
        }

        [HttpPost("query-by-receipt-no")]
        public async Task<ResponseEntity<PaymentReceipt[]>> QueryByReceiptNo([FromBody] ReceiptNoDto dto)
        {
            dto.Should().NotBeNull();
            dto.receiptNo.Should().NotBeNullOrEmpty();
            var query = this.SingleStoreService.ApplyToQueryable(this.Repo);
            var data = await query.Where(x => x.receiptNo == dto.receiptNo).ToArrayAsync();

            return new ResponseEntity<PaymentReceipt[]>().SetData(data);
        }
    }
}
